package db

import (
	"database/sql"
	"d-serv-go/server/core/config"
	"d-serv-go/server/core/log"
	_ "github.com/go-sql-driver/mysql"
)

var dsMap = map[string]*sql.DB{}

type Ds struct {
	url      string
	userName string
	passWord string
	host     string
	port     int
}

func GetDb(ds string) *sql.DB {
	return dsMap[ds]
}

func LoadDataSource() {
	sources := config.Instance.DataSources
	// dsn "xd:8emExa6A!@tcp(172.16.22.156:3306)/xd_steel_cord"
	for _, ds := range sources {
		db, err := sql.Open("mysql", ds.DataSourceName)
		if err != nil {
			panic(err)
		}
		db.SetMaxOpenConns(100) // 设置连接池最大连接数
		db.SetMaxIdleConns(20)  // 设置连接池最大空闲连接数
		dsMap[ds.Name] = db
		log.Info("load db:", ds.Name, "dsn:", ds.DataSourceName)
	}
}

var executortMap = map[string]SqlExecutorInterface{}

const (
	DB_TYPE   = "DB_TYPE"
	//
	MYSQL   = "MYSQL"
	MSSQL   = "MSSQL"
	ORCLE   = "ORCLE"
	POSTGRE = "POSTGRE"
	INFLUX  = "INFLUX"
	TAOS  = "TAOS"
)

func RegistExecutor() {
	executortMap[MYSQL] = &MysqlExecutor{
		SqlExecutor{},
	}
	executortMap[MSSQL] = &MssqlExecutor{
		SqlExecutor{},
	}
	executortMap[ORCLE] = &OracleExecutor{
		SqlExecutor{},
	}
	executortMap[POSTGRE] = &PostgreExecutor{
		SqlExecutor{},
	}
	executortMap[INFLUX] = &InfluxExecutor{
		SqlExecutor{},
	}
	executortMap[TAOS] = &TaosExecutor{
		SqlExecutor{},
	}
}

func GetExecutor(dbtype string) SqlExecutorInterface {
	return executortMap[dbtype]
}
